package com.chuangjiangx.member.manager.client.web.score.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.request.Page;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlinePageResponse;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.client.web.score.request.ScoreDetailForClientRequest;
import com.chuangjiangx.member.manager.client.web.score.request.ScoreDetailForClientStoreRequest;
import com.chuangjiangx.member.manager.client.web.score.request.SearchScoreInfoRequest;
import com.chuangjiangx.member.manager.client.web.score.response.AppScoreDetailResponse;
import com.chuangjiangx.member.manager.client.web.score.response.ClientScoreDetailReponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.score.ddd.dal.condition.ScoreStreamForClientCondition;
import com.chuangjiangx.member.score.ddd.query.MbrScoreStreamQuery;
import com.chuangjiangx.member.score.ddd.query.dto.AppScoreDetailDTO;
import com.chuangjiangx.member.score.ddd.query.dto.ClientScoreDTO;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 当前会员积分明细
 */
@Api(value = "当前会员积分明细",tags = "积分")
@RestController
@RequestMapping(value = "/app/member/personalScore", produces = "application/json")
public class MbrScorePersonalController {
    @Autowired
    private MbrScoreStreamQuery scoreStreamQuery;

    /**
     * 查询当前会员积分明细列表--商户权限
     *
     * @param clientRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "查询指定会员积分明细列表--商户权限")
    @RequestMapping(value = "/searchScoreDetailList",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("55002")
    public UnderlinePageResponse searachScoreDetailList(@Validated @RequestBody ScoreDetailForClientRequest clientRequest, HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        //对象转换
        ScoreStreamForClientCondition clientCondition = new ScoreStreamForClientCondition();
        BeanUtils.convertBean(clientRequest, clientCondition);
        clientCondition.setMemberId(clientRequest.getMemberId());
        clientCondition.setMerchantId(user.getMerchantId());

        //获取分页信息
        Page page = new Page();
        page.setPageNO(clientRequest.getPageNumber());
        page.setEveryPageCount(clientRequest.getPageSize());

        PagingResult<ClientScoreDTO> clientScoreDetailDTO = scoreStreamQuery.searchClientScoreDetailList(clientCondition);
        List<ClientScoreDetailReponse> respList = BeanUtils.convertCollection(clientScoreDetailDTO.getItems(), ClientScoreDetailReponse.class, null);

        return (UnderlinePageResponse) ResponseUtils.successPage(page, clientScoreDetailDTO, "memberScoreInfoList", respList);
    }



    /**
     * 根据id查询积分明细详情
     *
     * @param request 积分明细ID
     * @return 积分详情
     * @see AppScoreDetailResponse
     */
    @ApiOperation(value = "根据id查询积分明细详情")
    @RequestMapping(value = "/searchScoreInfo",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("59016")
    public UnderlineResponse<AppScoreDetailResponse> searchScoreInfoById(@Validated @RequestBody SearchScoreInfoRequest request) {
        AppScoreDetailResponse response = new AppScoreDetailResponse();
        AppScoreDetailDTO appScoreDetailDTO = scoreStreamQuery.getScoreDetailById(request.getId());
        BeanUtils.convertBean(appScoreDetailDTO, response);
        return (UnderlineResponse<AppScoreDetailResponse>)ResponseUtils.success("scoreInfo", response);
    }


    /**
     * 查询当前会员积分明细列表--门店权限
     *
     * @param clientRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "查询指定会员积分明细列表--门店权限")
    @RequestMapping(value = "/searchScoreDetailListForStore",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("56002")
    public UnderlinePageResponse searachScoreDetailListStore(@Validated @RequestBody ScoreDetailForClientStoreRequest clientRequest, HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        //对象转换
        ScoreStreamForClientCondition clientCondition = new ScoreStreamForClientCondition();
        BeanUtils.convertBean(clientRequest, clientCondition);
        clientCondition.setMemberId(clientRequest.getMemberId());
        clientCondition.setMerchantId(user.getMerchantId());
        clientCondition.setRegisterStoreId(user.getStoreId());
        //获取分页信息
        Page page = new Page();
        page.setPageNO(clientRequest.getPageNumber());
        page.setEveryPageCount(clientRequest.getPageSize());
        PagingResult<ClientScoreDTO> clientScoreDetailDTO = scoreStreamQuery.searchClientScoreDetailList(clientCondition);
        List<ClientScoreDetailReponse> respList = BeanUtils.convertCollection(clientScoreDetailDTO.getItems(), ClientScoreDetailReponse.class, null);

        return (UnderlinePageResponse)ResponseUtils.successPage(page, clientScoreDetailDTO, "memberScoreInfoList", respList);
    }

}
